package com.chenyue.cm.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chenyue.cm.base.aop.Log;
import com.chenyue.cm.base.bean.PageVo;
import com.chenyue.cm.base.bean.Pageable;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.utils.RequestUtil;
import com.chenyue.cm.msg.domain.JdMsg;
import com.chenyue.cm.user.service.ViewThirdApplyService;
import com.chenyue.cm.user.domain.ViewThirdApply;
import com.chenyue.cm.user.domain.ViewThirdApplyExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
* @author chenyue-cm
* @date 2021-10-31
*/
@RestController
@RequestMapping("/viewThirdApply")
public class ViewThirdApplyController {

    private final ViewThirdApplyService viewThirdApplyService;

    public ViewThirdApplyController(ViewThirdApplyService viewThirdApplyService) {
        this.viewThirdApplyService = viewThirdApplyService;
    }

    @RequestMapping("/getList")
    public Success getList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
		PageVo<ViewThirdApply>  result  = viewThirdApplyService.getPageBean(pageInfo, ViewThirdApply.class);
		return Success.success(result);
    }

    @RequestMapping("/getViewThirdApplyList")
    public Success getJdMsgList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        //设置分页信息
        PageHelper.startPage(pageInfo.getPageIndex(), pageInfo.getPageSize(), true);
        LambdaQueryWrapper<ViewThirdApply> wrapper = new LambdaQueryWrapper<>();
        Map<String, Object> pageSearch = pageInfo.getPageSearch();
        if (pageSearch != null) {
            if (null != pageSearch.get("SEARCH_LIKE_user_name")) {
                wrapper.like(ViewThirdApply::getUserName, String.valueOf(pageSearch.get("SEARCH_LIKE_user_name")));
            }
            if (null != pageSearch.get("SEARCH_EQ_apply_type")) {
                wrapper.eq(ViewThirdApply::getApplyType, String.valueOf(pageSearch.get("SEARCH_EQ_apply_type")));
            }
            if (null != pageSearch.get("SEARCH_LIKE_mobile")) {
                wrapper.eq(ViewThirdApply::getMobile, String.valueOf(pageSearch.get("SEARCH_LIKE_mobile")));
            }
        }
        wrapper.orderByDesc(ViewThirdApply::getCt);
        List<ViewThirdApply> list = viewThirdApplyService.list(wrapper);
        //封装返回结果
        PageInfo<ViewThirdApply> result = new PageInfo<>(list);

        return Success.success(result);
    }

    /**
     * @param vo 插入实体
     *
     * @return Success
     */
    @Log
    @RequestMapping("/save")
    public Success save(ViewThirdApply vo) {
        boolean success = viewThirdApplyService.save(vo);
        return success ? Success.success() : Success.error();
    }

    /**
    * @param vo 修改实体
    * @param request id
    *
    * @return Success
    */
   @Log
   @RequestMapping("/edit")
   public Success edit(ViewThirdApply vo, HttpServletRequest request) {
       String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            int success = viewThirdApplyService.getBaseMapper().updateById(vo);
            return success > 0 ? Success.success() : Success.error();
        } else {
          return Success.error("id不能为空");
        }
    }

    /**
     *
     * @param id 主键
     *
     * @return Success
     */
    @Log
    @RequestMapping("/delete/{id}")
    public Success deleteByPrimaryKey(@PathVariable("id") Integer id) {
        ViewThirdApplyExample aExample = new ViewThirdApplyExample();
        aExample.createCriteria().andIdEqualTo(id);
        int success = viewThirdApplyService.deleteByExample(aExample);
        return success > 0 ? Success.success() : Success.error();
    }

 	/**
	 * 【API】----  删除多个数据
     *
	 * @return Success
	 */
    @Log
    @RequestMapping("/delete")
	public Success delete(String ids) {
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            int success = viewThirdApplyService.deleteByPrimaryKey(idString);
            if (success == 0) {
                return Success.error();
            }
        }
        return Success.success();
	}

}
